#!/usr/bin/python3

import traceback
import time
from pydes.simulation import *

if __name__ == '__main__':
	Simulation.manager = Manager( )
	Simulation.obj_map = Simulation.manager.dict( )

	class Test (Simulation_Object):
		def __init__( self, name ):
			Simulation_Object.__init__( self )
			self.z = 2
			self.name = name

		def f( self, x, ot ):
			print( 't=%d: %s.f( %s )' % (get_time( ), self.name, x) )
			if x == 4:
				t = get_time( )
				future( self, t+5 ).z = self.name
				schedule(
					future( self, t+10, autoschedule = False ).f( x+1, t2 ),
					future( t2, t+11, autoschedule = False ).f( x+3, None ),
					future( self, t+11, autoschedule = False ).f( x+2, t2 ) )

		def __repr__( self ):
			return "%s, z=%s" % (self.name, self.z)

	try:
		t1 = Test( 't1' )
		t2 = Test( 't2' )
		s1 = Simulation( t1 )
		s2 = Simulation( t2 )
		future( t1, 10 ).f( 4, t2 )
		time.sleep( 1 )

	except:
		traceback.print_exc( )
	
	print( '%s' % t1 )
	t1 = s1.remove_object( t1 )
	print( '%s' % t1 )

	print( '%s' % t2 )
	t2 = s2.remove_object( t2 )
	print( '%s' % t2 )

	s1.kill( )
	s2.kill( )
	s1.join( )
	s2.join( )
